package com.Smart.smartscals.Utils;

import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.os.Environment;
import android.os.StatFs;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.Smart.smartscals.Activity.ActivityGuide;
import com.Smart.smartscals.Activity.ActivitySetUserInformation;
import com.Smart.smartscals.Activity.ActivityStartUsing;
import com.Smart.smartscals.Activity.ActivityWelcome;
import com.Smart.smartscals.Activity.R;
import com.Smart.smartscals.Activity.SmartScalsApplication;
import com.Smart.smartscals.Entity.EntityConversionTime;
import com.Smart.smartscals.Fragment.BaseFragment;
import com.Smart.smartscals.Fragment.FragmentGudeSex;

public class Tools {
	/**
	 * 打开Activity
	 * 
	 * @param mContext
	 * @param class1
	 */
	public static void startActivity(Activity mActivity, Context mContext,
			Class classTmp) {
//		mActivity.finish();
		Intent mIntent = new Intent(mContext, classTmp);
		mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(mIntent);
		Tools.setLog("---->startActivity---1");
		// mActivity.overridePendingTransition(R.anim.ani_translate_left,
		// R.anim.ani_translate_right);

	}

	/**
	 * 
	 */
	public static void setLog(String str) {
		Log.v("msg", str);

	}

	public static void setToast(Context mContext, String str) {

		Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();

	}

	/**
	 * 添加Fragmengt
	 * 
	 * @param mActivityGuide
	 * @param baseFragment
	 */
	public static void startFragment(ActivityGuide mActivityGuide,
			BaseFragment baseFragment) {

		mActivityGuide.setmFragmentTransaction(mActivityGuide
				.getSupportFragmentManager().beginTransaction());

		mActivityGuide.getmFragmentTransaction().setTransition(
				FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

		mActivityGuide.getmFragmentTransaction().setCustomAnimations(
				R.anim.ani_translate_left, R.anim.ani_translate_right);
		mActivityGuide.getmFragmentTransaction().add(
				R.id.activity_welcome_framelayout_all, baseFragment);

		mActivityGuide.getmFragmentTransaction().addToBackStack(null);
		mActivityGuide.setBaseFragmentLastTime(baseFragment);
		mActivityGuide.getmFragmentTransaction().commit();

	}

	/**
	 * 获取当前日期的实体
	 * 
	 * @return
	 */
	public static EntityConversionTime getCurrentTimeEntity() {

		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		EntityConversionTime entityConversionTime = new EntityConversionTime();
		entityConversionTime.setYearNUM(c.get(Calendar.YEAR));// 获取当前年份
		entityConversionTime.setMonthNUM(c.get(Calendar.MONTH) + 1);// 获取当前月份
		entityConversionTime.setDayNUM(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		int tmp = 1;
		if ("1".equals(mWay)) {
			tmp = 7;
		} else if ("2".equals(mWay)) {
			tmp = 1;
		} else if ("3".equals(mWay)) {
			tmp = 2;
		} else if ("4".equals(mWay)) {
			tmp = 3;
		} else if ("5".equals(mWay)) {
			tmp = 4;
		} else if ("6".equals(mWay)) {
			tmp = 5;
		} else if ("7".equals(mWay)) {
			tmp = 6;
		}

		entityConversionTime.setWeekNUM(tmp);
		tmp = entityConversionTime.getMonthNUM();
		if (tmp == 1 || tmp == 3 || tmp == 5 || tmp == 7 || tmp == 8
				|| tmp == 10 || tmp == 12) {// 31天
			tmp = 31;
			Tools.setLog("---->date1--:" + tmp);
		} else if (tmp == 2) {
			if (isLeapYear(entityConversionTime)) {
				tmp = 28;
			} else {
				tmp = 27;
			}
			Tools.setLog("---->date2--:" + tmp);
		} else {
			tmp = 30;
			Tools.setLog("---->date3--:" + tmp);
		}

		entityConversionTime.setMonthAllday(tmp);

		Tools.setLog("---->date4--:" + entityConversionTime.getDayNUM() + " "
				+ entityConversionTime.getMonthAllday() + "  "
				+ entityConversionTime.getMonthNUM() + "  "
				+ entityConversionTime.getWeekNUM() + "  "
				+ entityConversionTime.getYearNUM() + "  ");
		return entityConversionTime;
	}

	/**
	 * 获取一周的星期一(具体日期)；
	 * 
	 * @param conversionTime
	 * @return
	 */
	public static EntityConversionTime getWeekFirstDay(
			EntityConversionTime conversionTime) {

		EntityConversionTime conversionTimeTmp = new EntityConversionTime();
		if (conversionTime.getDayNUM() - conversionTime.getWeekNUM() >= 0) {// 表示这个星期的第一天就在这个月
			conversionTimeTmp.setMonthAllday(conversionTime.getMonthAllday());
			conversionTimeTmp.setWeekNUM(1);
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM());
			conversionTimeTmp.setMonthNUM(conversionTime.getMonthNUM());
			conversionTimeTmp.setDayNUM(conversionTime.getDayNUM()
					- conversionTime.getWeekNUM() + 1);
			Tools.setLog("--->getDate:1:"
					+ (conversionTime.getDayNUM() - conversionTime.getWeekNUM() + 1));
			return conversionTimeTmp;

		} else {// 这个星期的第一天不再这个月
			Tools.setLog("--->getDate:2");

			conversionTimeTmp.setMonthNUM(getBeforeMonth(conversionTime));

			// 设置月份
			if (conversionTimeTmp.getMonthNUM() == 12) {

				conversionTimeTmp
						.setYearNUM(conversionTimeTmp.getYearNUM() - 1);

			} else {

				conversionTimeTmp.setYearNUM(conversionTimeTmp.getYearNUM());

			}
			// 设置一个月的天数；
			conversionTimeTmp.setMonthAllday(getMonthDay(conversionTimeTmp));
			// 设置星期
			conversionTimeTmp.setWeekNUM(1);
			conversionTimeTmp
					.setDayNUM((getLastMontAllDay(conversionTime)
							.getMonthAllday() - (conversionTime.getWeekNUM() - conversionTime
							.getDayNUM())) + 1);
			return conversionTimeTmp;

		}

	}

	private static int getBeforeMonth(EntityConversionTime conversionTime) {
		int ret = 0;
		if (conversionTime.getMonthNUM() == 1) {
			ret = 12;
		} else {
			ret = conversionTime.getMonthNUM() - 1;
		}
		return ret;
	}

	/**
	 * 设置上个月的天数；
	 * 
	 * @param conversionTime
	 * @return
	 */
	private static EntityConversionTime getLastMontAllDay(
			EntityConversionTime conversionTime) {
		int tmp;
		EntityConversionTime conversionTimeTmp = new EntityConversionTime();

		if (conversionTime.getMonthNUM() == 1) {
			tmp = 12; // 12月
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM() - 1);// 设置年份
		} else {
			tmp = conversionTime.getMonthNUM() - 1;
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM());
		}

		conversionTimeTmp.setMonthNUM(tmp);
		if (tmp == 1 || tmp == 3 || tmp == 5 || tmp == 7 || tmp == 8
				|| tmp == 10 || tmp == 12) {// 31天
			tmp = 31;
		} else if (tmp == 2) {
			if (isLeapYear(conversionTime)) {
				tmp = 29;
			} else {
				tmp = 28;
			}
		} else {
			tmp = 30;
		}
		conversionTimeTmp.setMonthAllday(tmp);
		return conversionTimeTmp;
	}

	/**
	 * 设置上个月的天数；
	 * 
	 * @param conversionTime
	 * @return
	 */
	private static EntityConversionTime getNextMontAllDay(
			EntityConversionTime conversionTime) {
		int tmp;
		EntityConversionTime conversionTimeTmp = new EntityConversionTime();

		if (conversionTime.getMonthNUM() == 12) {
			tmp = 1; // 12月
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM() + 1);// 设置年份
		} else {
			tmp = conversionTime.getMonthNUM() + 1;
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM());
		}

		conversionTimeTmp.setMonthNUM(tmp);

		conversionTimeTmp.setMonthAllday(getMonthDay(conversionTimeTmp));
		return conversionTimeTmp;
	}

	private static int getMonthDay(EntityConversionTime conversionTimeTmp) {
		int tmp = conversionTimeTmp.getMonthNUM();

		if (tmp == 1 || tmp == 3 || tmp == 5 || tmp == 7 || tmp == 8
				|| tmp == 10 || tmp == 12) {// 31天
			tmp = 31;
		} else if (tmp == 2) {
			if (isLeapYear(conversionTimeTmp)) {
				tmp = 29;
			} else {
				tmp = 28;
			}
		} else {
			tmp = 30;
		}

		return tmp;
	}

	/**
	 * 判断是否为闰年 false = not leap
	 */
	private static boolean isLeapYear(EntityConversionTime conversionTime) {
		boolean ret;
		if (conversionTime.getYearNUM() / 4 == 0
				&& conversionTime.getYearNUM() / 100 != 0) {//

			ret = true;

		} else {
			ret = false;
		}
		return ret;
	}

	/**
	 * 获取一周的最后一天；
	 * 
	 * @param conversionTime
	 * @return
	 */
	public static EntityConversionTime getWeekLastDay(
			EntityConversionTime conversionTime) {
		EntityConversionTime conversionTimeTmp = new EntityConversionTime();
		if (7 - conversionTime.getWeekNUM() + conversionTime.getDayNUM() <= conversionTime
				.getMonthAllday()) {// 表示这个星期天就在这个月

			conversionTimeTmp.setMonthAllday(conversionTime.getMonthAllday());
			conversionTimeTmp.setWeekNUM(7);
			conversionTimeTmp.setYearNUM(conversionTime.getYearNUM());
			conversionTimeTmp.setMonthNUM(conversionTime.getMonthNUM());
			conversionTimeTmp.setDayNUM(7 - conversionTime.getWeekNUM()
					+ conversionTime.getDayNUM());
			return conversionTimeTmp;

		} else {// 这个星期的第一天不再这个月

			conversionTimeTmp.setMonthNUM(getNextMoth(conversionTime));
			// 设置月份
			if (conversionTimeTmp.getMonthNUM() == 1) {

				conversionTimeTmp.setYearNUM(conversionTime.getYearNUM());

			} else {

				conversionTimeTmp.setYearNUM(conversionTime.getYearNUM() + 1);

			}
			// 设置一个月的天数；
			conversionTimeTmp.setMonthAllday(getMonthDay(conversionTimeTmp));
			// 设置星期
			conversionTimeTmp.setWeekNUM(7);

			conversionTimeTmp.setDayNUM(7
					- conversionTime.getWeekNUM()
					- (conversionTime.getMonthAllday() - conversionTime
							.getDayNUM()));
			return conversionTimeTmp;

		}
	}

	private static int getNextMoth(EntityConversionTime conversionTime) {
		int ret = 0;
		if (conversionTime.getMonthNUM() == 12) {
			ret = 1;
		} else {
			ret = conversionTime.getMonthNUM() + 1;
		}
		return ret;
	}

	public static ArrayList<Bitmap> getArrlistBitMap(
			ArrayList<String> arrayListStr) {

		ArrayList<Bitmap> arrayList = new ArrayList<Bitmap>();

		Bitmap tmpBitmap = BitmapFactory.decodeResource(
				SmartScalsApplication.mContext.getResources(),
				R.drawable.kd_bitmap).copy(Bitmap.Config.ARGB_8888, true);
		for (int i = 0; i < arrayListStr.size(); i++) {
			Bitmap tmp = getBitmap(arrayListStr.get(i), tmpBitmap);
			arrayList.add(tmp);
		}

		return arrayList;
	}

	private static Bitmap getBitmap(String string, Bitmap tmpBitmap) {

		return CreateBitMap(tmpBitmap, string);
	}

	/**
	 * 
	 * @param bitmap
	 *            每张图都用这个画布
	 * @param arrayListTmp
	 *            每张画布要显示的 数值（就是刻度）
	 * @return 返回一个要设置的画布数组
	 */
	public static Bitmap CreateBitMap(Bitmap bitmap, String kd) {

		Paint paint = new Paint();
		Canvas canvas = new Canvas(bitmap);

		paint.setTextSize(60);
		// 去除毛边
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setTypeface(Typeface.SERIF);
		paint.setTextAlign(Align.CENTER);
		paint.setColor(Color.WHITE);

		int height = bitmap.getHeight();
		int width = bitmap.getWidth();

		FontMetrics fontMetrics = paint.getFontMetrics();
		// 计算文字高度
		float fontHeight = fontMetrics.bottom - fontMetrics.top;
		// 计算文字baseline
		float textBaseY = height - (height - fontHeight) / 2
				- fontMetrics.bottom;

		canvas.drawText(kd + "", width / 2, textBaseY, paint);

		canvas.save();
		canvas.restore();

		return bitmap;
	}

	public static void checkUpdate(Context mContext) {
		// TODO 自动生成的方法存根
		statBindSerVice();

	}

	private static void statBindSerVice() {

	}

	/**
	 * 获取内部版本号，这个
	 * 
	 * @param mContextTmp
	 * @return
	 */
	public static int getDeviceVersionCode(Context mContextTmp) {
		// TODO 自动生成的方法存根

		try {
			PackageInfo pi = mContextTmp.getPackageManager().getPackageInfo(
					mContextTmp.getPackageName(), 0);
			return pi.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}

	}

	/**
	 * 判断SD卡是否存在;
	 * 
	 * @return
	 */
	public static boolean isSdCardVisable() {
		// TODO 自动生成的方法存根

		return Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState());
	}

	/*
	 * 获取剩余空间,返回的单位；
	 * 
	 * //(availableBlocks * blockSize)/1024 KB 单位
	 * 
	 * //(availableBlocks * blockSize)/1024 /1024 MB单位
	 */
	public static long getSDOfferSize() {
		// TODO 自动生成的方法存根
		StatFs stat = new StatFs(getSdAbsolutePath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		Tools.setLog("--->state2--->:" + availableBlocks * blockSize);
		return availableBlocks * blockSize;
	}

	public static String getSdAbsolutePath() {

		// TODO 自动生成的方法存根
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	/**
	 * 获取下个月一号的时间Entitiy
	 * 
	 * @param conversionTimeLast
	 * 
	 * @return
	 */
	public static EntityConversionTime getNextMonthConversionEntitiy(
			EntityConversionTime conversionTimeLast) {

		EntityConversionTime mConversionTime = getNextMontAllDay(conversionTimeLast);
		mConversionTime.setDayNUM(1);
		return mConversionTime;
	}

	/**
	 * 获取上个月月底最后一天的的时间Entitiy
	 * 
	 * @param conversionTimeLast
	 * 
	 * @return
	 */
	public static EntityConversionTime getLastMonthConversionEntitiy(
			EntityConversionTime conversionTimeBefore) {

		EntityConversionTime mConversionTime = getLastMontAllDay(conversionTimeBefore);
		mConversionTime.setDayNUM(mConversionTime.getMonthAllday());
		return mConversionTime;
	}

	/**
	 * 这个用于判断用户名，密码的一些规则
	 * 
	 * @param editTextName
	 * @param editTextPassWord
	 * @return true 表示没问题，false 表示输入出错
	 */
	public static boolean isUserPassRightAble(EditText editTextName,
			EditText editTextPassWord) {
		boolean ret = false;
		
		return false;
	}
	/**
	 * 打开Activity
	 * 
	 * @param mContext
	 * @param class1
	 */

	
	public static void startActivityUnFinishActivity(Activity mActivity, Context mContext,
			Class classTmp) {
		Intent mIntent = new Intent(mContext, classTmp);
		mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(mIntent);
		Tools.setLog("---->startActivity---1");
		// mActivity.overridePendingTransition(R.anim.ani_translate_left,
		// R.anim.ani_translate_right);

	}
}
